package com.dl7.player.widgets;

import com.dl7.player.ResourceTable;
import com.dl7.player.utils.LogUtil;
import ohos.agp.components.*;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayManager;
import ohos.agp.window.service.Window;
import ohos.app.Context;
import ohos.media.image.PixelMap;
import ohos.utils.net.Uri;
import java.util.Optional;
import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_CONTENT;

/**
 * Created by long on 2016/11/17.
 */

public class ShareDialog extends CommonDialog {

    public static final float DIALOG_BOX_CORNER_RADIUS = 60.0f;
    public static final int DIALOG_BOX_WIDTH = 1210;
    public static final int DIALOG_BOX_HEIGHT = 930;

    private OnDialogClickListener mClickListener;
    private OnDialogDismissListener mDismissListener;
    private PixelMap mPixelMap;
    private boolean mIsShareMode = false;
    private Context mContext;
    private Image photo;
    private DirectionalLayout directionalLayout;
    private ComponentContainer componentContainer;

    public ShareDialog(Context context) {
        super(context);
        mContext = context;
        initComponents();
        setCornerRadius(DIALOG_BOX_CORNER_RADIUS);
        setSize(DIALOG_BOX_WIDTH, DIALOG_BOX_HEIGHT);
    }

    private void initComponents() {
        Component component = LayoutScatter.getInstance(mContext)
                .parse(ResourceTable.Layout_dialog_share, null, false);
        photo
                = (Image) component.findComponentById(ResourceTable.Id_iv_screenshot_photo);
        ComponentContainer.LayoutConfig layoutConfig = photo.getLayoutConfig();
        Optional<Display> display = DisplayManager.getInstance().getDefaultDisplay(mContext);
        layoutConfig.width = display.get().getAttributes().width * 7 / 10;
        layoutConfig.height = display.get().getAttributes().height * 7 / 10;
        photo.setLayoutConfig(layoutConfig);
        super.setContentCustomComponent(component);
        component.findComponentById(ResourceTable.Id_btn_cancel)
                .setClickedListener(new Component.ClickedListener() {
                    @Override
                    public void onClick(Component component) {
                        if (mDismissListener != null) {
                            mDismissListener.onDismiss();
                        }
                    }
                });

        Text tvShare = (Text) component.findComponentById(ResourceTable.Id_btn_share);
        if (mIsShareMode) {
            tvShare.setText("分享");
        }

        tvShare.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (mClickListener != null) {
                    mClickListener.onShare(mPixelMap, null);
                }
            }
        });
    }

//    @Override
//    protected void onCreate() {
//        super.onCreate();
//        Window window = this.getWindow();
////        window.addFlags(WindowManager.LayoutConfig.MARK_DIM_EVE_WINDOW_BEHIND);
//        directionalLayout = new DirectionalLayout(mContext);
//
//        window.setBackground("#00000000");
//
//    }

//    @Override
//    protected void onDestroy() {
//        super.onDestroy();
//        if (mDismissListener != null) {
//            mDismissListener.onDismiss();
//        }
//    }
//
//    public void dismiss() {
//        onDestroy();
//    }
//
//    @Override
//    protected void onHide() {
//        super.onHide();
//        if (mDismissListener != null) {
//            mDismissListener.onDismiss();
//        }
//    }

    public void setScreenshotPhoto(PixelMap pixelMap) {
        mPixelMap = pixelMap;
        if (mPixelMap != null) {
            photo.setPixelMap(mPixelMap);
        }
    }

    public void setClickListener(OnDialogClickListener clickListener) {
        mClickListener = clickListener;
    }

    public void setDismissListener(OnDialogDismissListener dismissListener) {
        mDismissListener = dismissListener;
    }

    public void setShareMode(boolean shareMode) {
        mIsShareMode = shareMode;
    }

    public interface OnDialogClickListener {
        void onShare(PixelMap bitmap, Uri uri);
    }

    public interface OnDialogDismissListener {
        void onDismiss();
    }
}
